home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / ostream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  16.6 KB  |  599 lines

  1. #ifndef __STD_OSTREAM__
  2. #define __STD_OSTREAM__
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * ostream - Declarations for the Standard Library ostream classes
  8.  *
  9.  * $Id: ostream,v 1.74 1996/09/24 18:59:20 smithey Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  14.  * ALL RIGHTS RESERVED *
  15.  * The software and information contained herein are proprietary to, and
  16.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  17.  * intends to preserve as trade secrets such software and information.
  18.  * This software is furnished pursuant to a written license agreement and
  19.  * may be used, copied, transmitted, and stored only in accordance with
  20.  * the terms of such license and with the inclusion of the above copyright
  21.  * notice.  This software and information or any other copies thereof may
  22.  * not be provided or otherwise made available to any other person.
  23.  *
  24.  * Notwithstanding any other lease or license that may pertain to, or
  25.  * accompany the delivery of, this computer software and information, the
  26.  * rights of the Government regarding its use, reproduction and disclosure
  27.  * are as set forth in Section 52.227-19 of the FARS Computer
  28.  * Software-Restricted Rights clause.
  29.  * 
  30.  * Use, duplication, or disclosure by the Government is subject to
  31.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  32.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  33.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  34.  * P.O. Box 2328, Corvallis, Oregon 97339.
  35.  *
  36.  * This computer software and information is distributed with "restricted
  37.  * rights."  Use, duplication or disclosure is subject to restrictions as
  38.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  39.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  40.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  41.  * then the "Alternate III" clause applies.
  42.  *
  43.  **************************************************************************/
  44.  
  45. #ifndef __STD_RWCOMPILER_H__ 
  46. #include <stdcomp.h>
  47. #endif
  48.  
  49. #include <ios>
  50. #include <streambuf>
  51.  
  52. #ifndef _RWSTD_NO_NAMESPACE
  53. namespace std {
  54. #endif
  55.  
  56. extern ostream _RWSTDExport cout;
  57. extern ostream _RWSTDExport cerr;
  58.  
  59. #ifndef _RWSTD_NO_WIDE_CHAR
  60. extern wostream _RWSTDExport wcout;
  61. extern wostream _RWSTDExport wcerr;
  62. #endif
  63.  
  64. template<class charT, class traits>
  65. class _RWSTDExportTemplate basic_ostream : virtual public basic_ios<charT, traits> {
  66.  
  67.   public:
  68.  
  69.       // for simplicity
  70.     typedef basic_ostream<charT, traits>           ostream_type;
  71.     typedef basic_ios<charT, traits>               ios_type;
  72.  
  73.     typedef traits                                 traits_type;
  74.     typedef charT                                  char_type;
  75.  
  76.     typedef _TYPENAME traits::int_type              int_type;
  77.     typedef _TYPENAME traits::pos_type              pos_type;
  78.     typedef _TYPENAME traits::off_type              off_type;
  79.  
  80.     _EXPLICIT basic_ostream(basic_streambuf<charT, traits> *sb);
  81.     virtual ~basic_ostream();
  82.  
  83.     class sentry {
  84.                   public:
  85.  
  86.                     inline _EXPLICIT
  87.                        sentry(basic_ostream<charT,traits>& stream)
  88.                        : stream_(stream)                  
  89.   {  
  90.             
  91.                       
  92.     #ifdef _RWSTD_MULTI_THREAD
  93.      #ifndef _RWSTD_NO_EXCEPTIONS
  94.       try {
  95.      #endif
  96.       if ( stream.rdbuf() )
  97.        {
  98.          _RWSTDGuard* tmp = new _RWSTDGuard(stream.rdbuf()->buffer_mutex_);
  99.          if ( tmp )
  100.           stream.ostream_sentry_guard = tmp;
  101.          else
  102.           stream.ostream_sentry_guard = 0;
  103.        }
  104.     #endif
  105.  
  106.     if (stream.tie()) 
  107.      stream.tie()->flush();
  108.  
  109.       if ( stream.is_synch() )
  110.       {
  111.         #ifndef _RWSTD_NO_WIDE_CHAR
  112.          if ( (((void *)&stream)==((void *)&cout)) || 
  113.               (((void *)&stream)==((void *)&wcout)) )
  114.         #else
  115.          if ( ((void *)&stream)==((void *)&cout) )
  116.         #endif
  117.          {
  118.             fflush(stdout);
  119.          }
  120.  
  121.         #ifndef _RWSTD_NO_WIDE_CHAR
  122.          if ( (((void *)&stream)==((void *)&cerr)) || 
  123.               (((void *)&stream)==((void *)&wcerr)) ) 
  124.         #else
  125.          if ( ((void *)&stream)==((void *)&cerr) )
  126.         #endif
  127.          {
  128.             fflush(stderr);
  129.          }
  130.        }
  131.  
  132.     if ( stream.rdbuf() )
  133.      {
  134.        if ( stream.rdbuf()->which_open_mode( ) & ios_base::app )
  135.         stream.rdbuf()->pubseekoff(0,ios_base::end,ios_base::out);
  136.      }
  137.  
  138.      ok_ = stream.good(); 
  139.  
  140.      if ( !ok_ ) stream.setstate(ios_base::badbit);
  141.  
  142.      #ifdef _RWSTD_MULTI_THREAD
  143.       #ifndef _RWSTD_NO_EXCEPTIONS
  144.        }
  145.        
  146.       catch(...)
  147.        {
  148.          if ( stream.ostream_sentry_guard )
  149.           {
  150.             delete stream.ostream_sentry_guard;
  151.             stream.ostream_sentry_guard = 0;
  152.             throw;
  153.            }
  154.          }
  155.        #endif
  156.       #endif
  157.  
  158.    }
  159.  
  160.                     ~sentry() {
  161.                       if( stream_.flags() & ios_base::unitbuf)
  162.                        {
  163.                         if ( stream_.rdbuf()->pubsync() == -1 )
  164.                             stream_.setstate(ios_base::badbit);
  165.                        }
  166.  
  167.                     if ( stream_.is_synch() )
  168.                      {
  169.                       #ifndef _RWSTD_NO_WIDE_CHAR
  170.                        if ( (((void *)&stream_)==((void *)&cout)) || 
  171.                             (((void *)&stream_)==((void *)&wcout))||
  172.                             (((void *)&stream_)==((void *)&cerr)) || 
  173.                             (((void *)&stream_)==((void *)&wcerr)) ) 
  174.                       #else
  175.                        if ( (((void *)&stream_)==((void *)&cout)) ||
  176.                             (((void *)&stream_)==((void *)&cerr)) ) 
  177.                       #endif
  178.                        {
  179.                          if ( stream_.rdbuf()->pubsync() == -1 )
  180.                           stream_.setstate(ios_base::badbit);
  181.                        }
  182.                       }
  183.  
  184.                       #ifdef _RWSTD_MULTI_THREAD
  185.                        if ( stream_.ostream_sentry_guard )
  186.                         {
  187.                           delete stream_.ostream_sentry_guard;
  188.                           stream_.ostream_sentry_guard = 0;
  189.                         }
  190.                       #endif
  191.                     }
  192.  
  193.                     operator bool () { return ok_; }
  194.  
  195.                     private:
  196.  
  197.                      basic_ostream<charT,traits>& stream_;
  198.                      bool ok_;
  199.                  };
  200.  
  201.     ostream_type& operator<<(ostream_type& (*pf)(ostream_type&));
  202.  
  203.     ostream_type& operator<<(ios_base& (*pf)(ios_base&));
  204.     ostream_type& operator<<(ios_type& (*pf)(ios_type&));
  205.  
  206. #ifndef _RWSTD_NO_BOOL
  207.     ostream_type& operator<<(bool n);
  208. #endif
  209.     ostream_type& operator<<(short n);
  210.     ostream_type& operator<<(unsigned short n);
  211.     ostream_type& operator<<(int n);
  212.     ostream_type& operator<<(unsigned int n);
  213.     ostream_type& operator<<(long n);
  214.     ostream_type& operator<<(unsigned long n);
  215.     ostream_type& operator<<(float f);
  216.     ostream_type& operator<<(double f);
  217.     ostream_type& operator<<(long double f); 
  218. #ifdef _RWSTD_LONG_LONG
  219.     ostream_type& operator<<(_RWSTD_LONG_LONG n); 
  220. #endif
  221.     ostream_type& operator<<(void *p);
  222.    
  223.     ostream_type& operator<<(basic_streambuf<char_type, traits>& sb)
  224.     {
  225.       ios_base::iostate err = 0;
  226.  
  227.       #ifndef _RWSTD_NO_EXCEPTIONS
  228.       try {
  229.       #endif
  230.  
  231.       if ( !(sb.which_open_mode() & ios_base::in) )
  232.         err = ios_base::failbit;
  233.       else
  234.       {
  235.  
  236.         _TYPENAME basic_ostream<charT, traits>::sentry opfx(*this);
  237.  
  238.         if(opfx) {
  239.              int_type    c;
  240.              while( !traits::eq_int_type( (c = sb.sbumpc()),traits::eof()) ) {
  241.               if( traits::eq_int_type(rdbuf()->sputc(c),traits::eof()) ) {
  242.                err = ios_base::failbit;
  243.                break;
  244.               }
  245.              }
  246.         }
  247.       }
  248.  
  249.       #ifndef _RWSTD_NO_EXCEPTIONS
  250.       }
  251.       #endif
  252.  
  253.       #ifndef _RWSTD_NO_EXCEPTIONS
  254.       catch(...)
  255.       {
  256.         bool flag = false;
  257.         try {
  258.             this->setstate(ios_base::badbit);
  259.         }
  260.         catch( ios_base::failure ) { flag= true; }
  261.         if ( flag ) throw;
  262.       }
  263.       #endif
  264.  
  265.       if ( err ) this->setstate(err);
  266.  
  267.       return *this;
  268.     }
  269.  
  270.     ostream_type& operator<<(basic_streambuf<char_type, traits> *sb)
  271.     {
  272.      ios_base::iostate err = 0; 
  273.  
  274.      #ifndef _RWSTD_NO_EXCEPTIONS
  275.      try {
  276.      #endif
  277.  
  278.      if (sb)
  279.      {
  280.  
  281.       if ( !(sb->which_open_mode() & ios_base::in) )
  282.         err = ios_base::failbit;
  283.       else
  284.       {
  285.  
  286.        _TYPENAME basic_ostream<charT, traits>::sentry opfx(*this);
  287.  
  288.        if(opfx) {
  289.                       int_type    c;
  290.               while( !traits::eq_int_type( (c = sb->sbumpc()),traits::eof())) {
  291.               if( traits::eq_int_type(rdbuf()->sputc(c),traits::eof())) {
  292.                err = ios_base::failbit;
  293.                break;
  294.              }
  295.             }
  296.           }
  297.         }
  298.       }
  299.       else 
  300.         err = ios_base::badbit;  
  301.  
  302.       #ifndef _RWSTD_NO_EXCEPTIONS
  303.       }
  304.       #endif
  305.  
  306.       #ifndef _RWSTD_NO_EXCEPTIONS
  307.       catch(...)
  308.       {
  309.             bool flag = false;
  310.         try {
  311.               this->setstate(ios_base::badbit);
  312.         }
  313.         catch( ios_base::failure ) { flag= true; }
  314.         if ( flag ) throw;
  315.       }
  316.       #endif
  317.  
  318.       if ( err ) this->setstate(err);
  319.  
  320.       return *this;
  321.     }
  322.  
  323.     ostream_type& put(char_type c);
  324.  
  325.     ostream_type& write(const char_type *s, streamsize n);
  326.  
  327.     ostream_type& flush();
  328.  
  329.     ostream_type& seekp(pos_type pos)
  330.     {
  331.       #ifdef _RWSTD_MULTI_THREAD
  332.        if ( rdbuf() )
  333.       #  ifndef __TURBOC__
  334.         _RWSTDGuard guard(this->rdbuf()->buffer_mutex_);
  335.       #  else
  336.         STDGUARD(this->rdbuf()->buffer_mutex_);
  337.       #  endif // __TURBOC__
  338.       #endif // _RWSTD_MULTI_THREAD
  339.  
  340.       if ( this->bad() ) return *this;
  341.  
  342.       if ( this->rdstate() & ios_base::eofbit )
  343.        clear( this->rdstate() & ~ios_base::eofbit );
  344.    
  345.       if( rdbuf()->pubseekpos(pos, ios_base::out) == pos_type(off_type(-1)) )
  346.        this->setstate(ios_base::failbit);
  347.  
  348.       return *this;
  349.     }
  350.  
  351.     ostream_type& seekp(off_type , ios_base::seekdir );
  352.     pos_type tellp();
  353.     
  354.     #ifdef _RWSTD_MULTI_THREAD
  355.      _RWSTDGuard *ostream_sentry_guard;
  356.     #endif
  357.  
  358.   protected:
  359.  
  360.    basic_ostream();
  361.  
  362. };
  363.  
  364. //
  365. // ostream_iterator
  366. //
  367.  
  368. template <class T, class charT, class traits>
  369. class _RWSTDExportTemplate ostream_iterator : public output_iterator
  370. {
  371. protected:
  372.  
  373.     basic_ostream<charT,traits>* stream;
  374.     const charT*    str;
  375.  
  376. public:
  377.     typedef T value_type;
  378.     typedef charT char_type;
  379.     typedef traits traits_type;
  380.     typedef basic_ostream<charT,traits> ostream_type;
  381.  
  382.     ostream_iterator (basic_ostream<charT,traits>& s) 
  383.       : stream(&s),str(0) 
  384.     { ; }
  385.     ostream_iterator (basic_ostream<charT,traits>& s,const charT* c) 
  386.       : stream(&s), str((charT *)c)  
  387.     { ; }
  388.     ostream_iterator ( const ostream_iterator<T,charT,traits>& x )
  389.       : stream(x.stream) , str(x.str)
  390.     { ; }
  391.     ostream_iterator<T,charT,traits>& operator= (const T& value)
  392.     { 
  393.         *stream << value;
  394.         if (str) *stream << str;
  395.         return *this;
  396.     }
  397.     ostream_iterator<T,charT,traits>& operator*  ()    { return *this; }
  398.     ostream_iterator<T,charT,traits>& operator++ ()    { return *this; } 
  399.     ostream_iterator<T,charT,traits>& operator++ (int) { return *this; }
  400. };
  401.  
  402.  
  403. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  404. typedef basic_ostream<char>                           ostream;
  405. #else
  406. typedef basic_ostream<char, char_traits<char> >        ostream;
  407. #endif
  408.  
  409. #ifndef _RWSTD_NO_WIDE_CHAR
  410. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  411. typedef basic_ostream<wchar_t>                        wostream;
  412. #else
  413. typedef basic_ostream<wchar_t, char_traits<wchar_t> >  wostream;
  414. #endif
  415. #endif
  416.  
  417. // charT and charT* insertors
  418.  
  419. template<class charT, class traits>
  420. basic_ostream<charT, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<charT, traits>&, 
  421.                                            charT );
  422.  
  423. #ifndef _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
  424.  
  425. template<class charT, class traits>
  426. basic_ostream<charT, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<charT, traits>&, 
  427.                                            char );
  428.  
  429. #ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
  430. template <class traits>
  431. basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&, 
  432.                                           char );
  433. #else
  434. ostream& operator<< ( ostream&, char );
  435. #endif
  436. #endif
  437.  
  438.  
  439. template<class charT, class traits>
  440. basic_ostream<charT, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<charT, traits>&, 
  441.                                            const charT* );
  442.  
  443. #ifndef _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
  444.  
  445. template<class charT, class traits>
  446. basic_ostream<charT, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<charT, traits>&, 
  447.                                            const char* );
  448.  
  449. #ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
  450. template <class traits>
  451. basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&, 
  452.                                           const char* );
  453. #else
  454. ostream& _RWSTDExport operator<< ( ostream&, const char* );
  455. #endif
  456. #endif
  457.  
  458.  
  459. // signed and unsigned insertors
  460.  
  461. #ifndef _RWSTD_NO_SIGNED_CHAR_IN_STREAMS
  462. template <class traits>
  463. basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&, 
  464.                                           unsigned char );
  465.  
  466. template <class traits>
  467. basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&, 
  468.                                           signed char );
  469.  
  470. template <class traits>
  471. basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&,
  472.                                           const unsigned char* );
  473.  
  474. template <class traits>
  475. basic_ostream<char, traits>& _RWSTDExportTemplate operator<< ( basic_ostream<char, traits>&,
  476.                                           const signed char* );
  477. #endif
  478.  
  479. // String insertors
  480.  
  481. #ifndef _MSC_VER
  482. template<class charT, class traits, class Allocator>
  483. inline basic_ostream<charT, traits>&
  484. _RWSTDExport operator<< (basic_ostream<charT,traits>& os,
  485.             const basic_string<charT,traits,Allocator>& s)
  486. {
  487.     os.write(s.data(), s.size() );
  488.     return os;
  489. }
  490. #else
  491. inline ostream& _RWSTDExport operator<< (ostream& os,
  492.             const string& s)
  493. {
  494.     os.write(s.data(), s.size() );
  495.     return os;
  496. }
  497.  
  498. #ifndef _RWSTD_NO_WIDE_CHAR
  499. inline wostream& _RWSTDExport operator<< (wostream& os,
  500.             const wstring& s)
  501. {
  502.     os.write(s.data(), s.size() );
  503.     return os;
  504. }
  505. #endif /* _RWSTD_NO_WIDE_CHAR */
  506. #endif /* _MSC_VER */
  507.  
  508. extern ostream _RWSTDExport cout;
  509. extern ostream _RWSTDExport cerr;
  510. extern ostream _RWSTDExport clog;
  511.  
  512. // declare a couple of standard manipulators
  513. // global functions
  514.  
  515.  
  516. #ifdef _MSC_VER
  517.  
  518. struct endl_type { };
  519. struct ends_type { };
  520. struct flush_type { };
  521.  
  522. extern endl_type _RWSTDExport endl;
  523. extern ends_type _RWSTDExport ends;
  524. extern flush_type _RWSTDExport flush;
  525.  
  526. template<class charT, class traits>
  527. inline basic_ostream<charT, traits>&
  528. operator<<(basic_ostream<charT, traits>& os, endl_type)
  529. {
  530.   os.put( charT('\n') );
  531.   os.flush();
  532.  
  533.   return os;
  534. }
  535.  
  536. template<class charT, class traits>
  537. inline basic_ostream<charT, traits>&
  538. operator<<(basic_ostream<charT, traits>& os, ends_type)
  539. {
  540.   os.put( charT(0) );
  541.   os.flush(); 
  542.  
  543.   return os;
  544. }
  545.  
  546. template<class charT, class traits>
  547. inline basic_ostream<charT, traits>&
  548. operator<<(basic_ostream<charT, traits>& os, flush_type)
  549. {
  550.   os.flush();
  551.  
  552.   return os;
  553. }
  554.  
  555. #else
  556.  
  557. template<class charT, class traits>
  558. inline basic_ostream<charT, traits>&
  559. endl(basic_ostream<charT, traits>& os)
  560. {
  561.   os.put( charT('\n') );
  562.   os.flush(); 
  563.  
  564.   return os;
  565. }
  566.  
  567.  
  568. template<class charT, class traits>
  569. inline basic_ostream<charT, traits>&
  570. ends(basic_ostream<charT, traits>& os)
  571. {
  572.   os.put( charT(0) );
  573.  
  574.   return os;
  575. }
  576.  
  577.  
  578. template<class charT, class traits>
  579. inline basic_ostream<charT, traits>&
  580. flush(basic_ostream<charT, traits>& os)
  581. {
  582.   os.flush();
  583.  
  584.   return os;
  585. }
  586.  
  587. #endif // __MSC_VER
  588.  
  589. #ifndef _RWSTD_NO_NAMESPACE
  590. }
  591. #endif
  592.  
  593. #ifdef _RWSTD_COMPILE_INSTANTIATE
  594. #include <ostream.cc>
  595. #endif
  596.  
  597. #pragma option pop
  598. #endif /* __OSTREAM__ */
  599.